package com.hspedu.newData.dmsxl.图;
/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-28 
 * @Version: 1.0
 */

public class LC463岛屿的周长 {

    // todo
    public int islandPerimeter(int[][] grid) {

        int m = grid.length;
        int n = grid[0].length;
        int sum = 0;
        int cover = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1){
                    sum++;
                    // 向上边查询
                    if (i>0 && grid[i-1][j] == 1) cover++;
                    // 向左边查询
                    if (j>0 && grid[i][j-1] == 1) cover++;
                }
            }
        }

        return sum*4-cover*2;
    }

}
